home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Problem with stringcopy
- Date: Sun, 07 Jan 96 00:10:07 GMT
- Organization: none
- Distribution: world
- Message-ID: <820973407snz@genesis.demon.co.uk>
- References: <4clguu$9fs@eagle.novo.dk> <6JAN199609050601@erich.triumf.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <6JAN199609050601@erich.triumf.ca>
- bennett@erich.triumf.ca "P.Bennett" writes:
-
- >In article <4clguu$9fs@eagle.novo.dk>, morb@novo.dk (Morten Brun) writes...
- >>How do i do a partial stringcopy i.e. copy from a specific position in
- >>a string and a certain numbers of bytes. I am looking for a function
- >>like target = Stringcopy(source, startpos, length)
- >>Can anybody help please.
- >
- >Try strncpy(dest, source, size)
- >
- >If you want to copy 5 chars starting at the 10th char, do something like:
- > strcpy(dest, source+10, 5);
-
- Was that meant to be strncpy?
-
- Anyway strncpy is a bad idea since it always writes exactly 'size'
- characters to the destination string. This means that if there are at
- least 'size' characters available before the null character in the
- source string, the destination string won't be null character terminated
- and hence not a string. If there are significantly less than 'size'
- characters in the source string it spends time null character filling
- all the extra ones in the destination string. My earlier suggestion
- using strncat doesn't suffer from these problems.
-
- Admittedly Morten didn't make clear that he wanted the target array to
- end up with a properly terminated string. If not strncpy may do the
- job required.
-
- >There are several string functions starting with "strn" that deal with a
- >specified number of chars, rather than with the whole string.
-
- memcpy may also be relevant but you'd have to bounds check for the end
- of the source string explicitly.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-